package com.lfnyxy.web.org;

import com.lfnyxy.common.LogTool;
import com.lfnyxy.common.dto.ApiResult;
import com.lfnyxy.common.dto.CodeMessage;
import com.lfnyxy.common.dto.PagedDataTable;
import com.lfnyxy.entity.org.UserFunctionDetailEntity;
import com.lfnyxy.entity.org.UserFunctionEntity;
import com.lfnyxy.query.org.UserFunctionQuery;
import com.lfnyxy.service.org.UserFunctionService;
import com.lfnyxy.vo.ButtnVo;
import com.lfnyxy.vo.PageVo;
import com.lfnyxy.vo.UserFunctionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by.
 */
@Controller
@RequestMapping("/userFunction")
@ResponseBody
public class UserFunctionController {
    @Autowired
    UserFunctionService userFunctionService;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult<Integer> CreateUserFunction(@RequestBody UserFunctionEntity item) {
        int result = 0;
        if (item != null) {
            try {
                if (item.getId() > 0) {
                    result = userFunctionService.UpdateItem(item);
                } else {
                    result = userFunctionService.CreateItem(item);
                }
            } catch (Exception ex) {
                LogTool.WriteError(this.getClass().toString() + "001", ex.getMessage());
                return ApiResult.error(CodeMessage.SAVE_FAILED);
            }
        }
        return ApiResult.success(result);
    }

    @RequestMapping(value = {"/plist"}, method = {RequestMethod.GET})
    @ResponseBody
    public PagedDataTable QueryPageList(@RequestParam(value = "start", required = false) int start, @RequestParam(value = "length", required = false) int length, @RequestParam(value = "matchString", required = false) String matchString) {
        List<UserFunctionEntity> result = null;
        UserFunctionQuery query = new UserFunctionQuery();
        query.setStart(start);
        query.setLength(length);
        query.setMatchString(matchString);
        try {
            result = userFunctionService.QueryPageList(query);
        } catch (Exception ex) {
            LogTool.WriteError(this.getClass().toString() + "002", ex.getMessage());
        }
        return new PagedDataTable<UserFunctionEntity>(query.getTotal(), result);
    }

    @RequestMapping(value = {"/item"}, method = {RequestMethod.POST})
    @ResponseBody
    public ApiResult<UserFunctionEntity> QueryItem(@RequestBody UserFunctionQuery query) {
        if (query != null && query.getId() > 0) {
            try {
                UserFunctionEntity result = userFunctionService.QueryItem(query.getId());
                return ApiResult.success(result);
            } catch (Exception ex) {
                LogTool.WriteError(this.getClass().toString() + "003", ex.getMessage());
            }
        }
        return ApiResult.error(CodeMessage.GET_ITEM_FAILED);
    }

    @RequestMapping(value = {"/ditem"}, method = {RequestMethod.POST})
    @ResponseBody
    public ApiResult<Integer> DeleteItem(@RequestBody UserFunctionQuery query) {
        try {
            if (query != null && query.getId() > 0) {
                int cnt = userFunctionService.DeleteItem(query.getId());
                return ApiResult.success(cnt);
            }
        } catch (Exception ex) {
            LogTool.WriteError(this.getClass().toString() + "004", ex.getMessage());
        }
        return ApiResult.error(CodeMessage.DELETE_ITEM_FAILED);
    }

    @RequestMapping(value = {"/all"}, method = {RequestMethod.POST})
    @ResponseBody
    public ApiResult<List<UserFunctionEntity>> QueryAll() {
        try {
            return ApiResult.success(userFunctionService.QueryAll());
        } catch (Exception ex) {
            LogTool.WriteError(this.getClass().toString() + "005", ex.getMessage());
            return ApiResult.error(CodeMessage.QUERY_LIST_FAILED);
        }
    }

    @RequestMapping(value = {"/savefuns"}, method = {RequestMethod.POST})
    @ResponseBody
    public ApiResult<Boolean> SaveFuns(@RequestBody UserFunctionVo vo) {
        if(vo != null){
            //TODO 先通过 roleID 去找是否已经存在。

            int funID = 0;
            UserFunctionEntity ufEntity = userFunctionService.QueryByRole(vo.getRoleId());

            if(ufEntity != null){
                funID = ufEntity.getId();
                userFunctionService.DeleteFunctionDetail(funID);
            }else{
                UserFunctionEntity entity = new UserFunctionEntity();
                entity.setRoleId(vo.getRoleId());
                entity.setType(vo.getType());
                entity.setCreateUser(vo.getCreateUser());
                funID = userFunctionService.CreateItem(entity);
            }

            if(vo.getPages() != null && vo.getPages().size() > 0){
                for(PageVo page : vo.getPages()){
                    UserFunctionDetailEntity detail = new UserFunctionDetailEntity();
                    detail.setFuntionId(funID);
                    detail.setModuleNo(page.getModuleNo());
                    detail.setPageNo(page.getPageNo());
                    detail.setButtonNos(getBtnNos(page.getPageBtns()));
                    userFunctionService.CreateFunctionDetail(detail);
                }
            }

            return ApiResult.success(true);
        }
        return ApiResult.error(CodeMessage.SAVE_FAILED);
    }

    @RequestMapping(value = {"/sall"}, method = {RequestMethod.POST})
    @ResponseBody
    public ApiResult<List<PageVo>> getFunctions(@RequestBody UserFunctionQuery query){
        if(query.getRoleId() > 0){
           return ApiResult.success( userFunctionService.QueryPageVoByRoleId(query.getRoleId()));
        }else{
            return ApiResult.error(CodeMessage.QUERY_PARAM_ERROR);
        }
    }

    private String getBtnNos(List<ButtnVo> btns){
        if(btns != null && btns.size() > 0){
            StringBuilder sb = new StringBuilder();
            for(ButtnVo vo : btns){
                if(sb.length() > 0){
                    sb.append("|");
                }
                sb.append(vo.getBtnNo());
            }
            return sb.toString();
        }
        return "";
    }

}